package com.zyd.recyclerviewdemo;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.zyd.recyclerviewdemo.adapter.RefreshRecycleAdapter;
import com.zyd.recyclerviewdemo.view.RefreshRecycleView;

import java.util.ArrayList;
import java.util.List;

public class RefreshActivity extends AppCompatActivity implements RefreshRecycleView.IOnScrollListener {

    private RefreshRecycleView recycleView;//下拉刷新RecycleView

    private List<String> list;//列表

    private RefreshRecycleAdapter adapter;//Adapter

    private AlertDialog.Builder builder;//提示框
    private Dialog dialogProgress;

    private static final int REFRESH_Load = 0;//下拉刷新

    private static final int MORE_Load = 1;//加载更多

    private LinearLayout loadMore;

    private Handler handler = new Handler(Looper.myLooper()) {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case REFRESH_Load:

                    recycleView.setLoadMoreEnable(true);

                    if (dialogProgress.isShowing()) {
                        dialogProgress.hide();
                    }

                    if (list != null) {
                        list.clear();
                    }

                    loadData();

                    adapter.notifyDataSetChanged();

                    break;

                case MORE_Load:

                    recycleView.setLoadMoreEnable(false);

                    if (dialogProgress.isShowing()) {
                        dialogProgress.hide();
                    }

                    loadData();

                    adapter.notifyDataSetChanged();

                    break;

            }

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refresh);
        initView();
    }

    public void initView() {

        builder = new AlertDialog.Builder(this);
        setDialog(false);

        loadMore = findViewById(R.id.loadMore);

        list = new ArrayList<>();

        loadData();

        recycleView = findViewById(R.id.main_recycle_view_data);

        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RefreshActivity.this);

        recycleView.setLayoutManager(linearLayoutManager);

        adapter = new RefreshRecycleAdapter(RefreshActivity.this, list);

        recycleView.setAdapter(adapter);

        recycleView.setListener(this);

        recycleView.setRefreshEnable(true);

        recycleView.setLoadMoreEnable(true);

    }

    /**
     * 设置Dialog
     *
     * @param isShow
     */
    private void setDialog(boolean isShow) {
        builder.setView(R.layout.layout_progress);
        dialogProgress = builder.create();
        dialogProgress.setCanceledOnTouchOutside(false);
        if (isShow) {
            dialogProgress.show();
        } else {
            dialogProgress.dismiss();
        }
    }

    /**
     * 加载数据
     */

    public void loadData() {

        for (int i = 0; i < 10; i++) {

            list.add("第" + i + "行");

        }

    }

    @Override
    public void onRefresh() {

        if (!dialogProgress.isShowing()) {
            dialogProgress.show();
        }

        new Thread() {

            @Override
            public void run() {
                super.run();

                try {

                    sleep(3000);

                    handler.sendEmptyMessage(REFRESH_Load);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }.start();

    }

    @Override
    public void onLoadMore() {

        if (!dialogProgress.isShowing()) {
            dialogProgress.show();
        }

        new Thread() {

            @Override
            public void run() {
                super.run();

                try {

                    sleep(3000);

                    handler.sendEmptyMessage(MORE_Load);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }.start();

    }

    @Override
    public void onLoaded() {

        Toast.makeText(RefreshActivity.this, "Loaded all", Toast.LENGTH_SHORT).show();

    }

}